/**   
 * @Title: LoginActivity.java 
 * @Package: com.rename.icode.activity 
 * @Description: Administrator版权所有
 * @author Administrator  
 * @date 2015-2-12 下午1:24:14 
 * @version  
 */

package com.rename.icode.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.rename.icode.R;
import com.rename.icode.manager.AppConfigManager;
import com.rename.icode.task.LoginTask;
import com.rename.icode.utils.LogUtil;
import com.rename.icode.view.ClearEditText;

/**
 * @Description 实现了...功能
 * @author Administrator
 * @date 2015-2-12 下午1:24:14
 * @version V1.0
 */

public class LoginActivity extends ActivitySupport {

	private static final String TAG = "LoginActivity";

	private ClearEditText mUsernameText, mPasswdText;
	private Button mLoginCfmBtn = null;
	private ImageView loginImg = null;
	private long mExitTime = 0;
	private View mForgetPsdTextVeiw;
	
	private final static int LOGIN_FAIL = 0;
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOGIN_FAIL:
//				((AnimationDrawable) loginImg.getBackground()).stop(); // 停止登录动画
				// 登录按钮恢复原状
				mLoginCfmBtn.setBackgroundResource(R.drawable.green_button_frame);
				mLoginCfmBtn.setClickable(true);
				// 提示用户
				showToast("您的用户名或者密码有误", 0);
				mUsernameText.setText("");
				mPasswdText.setText("");
				// 设置焦点
				mUsernameText.setFocusable(true);
				mUsernameText.setFocusableInTouchMode(true);
				mUsernameText.requestFocus();
				mUsernameText.requestFocusFromTouch();
				break;

			default:
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);

		LogUtil.i(TAG, "登录LoginActivity");

		init();
	}

	protected void init() {
		getICodeApplication().addActivity(this);

		mUsernameText = (ClearEditText) findViewById(R.id.login_name_input);
		mPasswdText = (ClearEditText) findViewById(R.id.login_password_input);
		mLoginCfmBtn = (Button) findViewById(R.id.login_cfm_btn);
		loginImg = (ImageView) findViewById(R.id.loading_img);
		mForgetPsdTextVeiw = (TextView) findViewById(R.id.forget_password);
		
		mLoginCfmBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String username = mUsernameText.getText().toString().trim();
				String password = mPasswdText.getText().toString().trim();

				System.out.println("username:" + username + "/" + "password:"
						+ password);

				AppConfigManager.getInstance().setPassword(password);
				AppConfigManager.getInstance().setUsername(username);

				loginImg.setVisibility(View.VISIBLE);

				LoginTask loginTask = new LoginTask(LoginActivity.this,
						(AnimationDrawable) loginImg.getBackground());
				loginTask.execute();

				mLoginCfmBtn
						.setBackgroundResource(R.drawable.green_button_frame_pressed);
				mLoginCfmBtn.setClickable(false);
			}
		});
		mForgetPsdTextVeiw.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				 Intent intent = new Intent(LoginActivity.this,
				 ResetPasswordActivity.class);
				 startActivity(intent);
			}
		});
	}
	
	public void notifyLoginFail(){
		mHandler.sendEmptyMessage(LOGIN_FAIL);
	}

	@Override
	public void resetLoginBtn() {
		mLoginCfmBtn.setClickable(true);
		mLoginCfmBtn.setBackgroundResource(R.drawable.green_button_frame);
		mPasswdText.setText("");
	}

	@Override
	public void hideLoginAnim() {
		loginImg.setVisibility(View.GONE);
	}
	
}
